<?php

/**
 * Description of ContentAdminLanguages
 *
 * @author Honza, Tung
 */
class ContentAdminLanguages implements IMainBodyWithInteraction {

  private $showTable = true;	//show data which is selected from DB  
  private $showForm = true;		//show form for add or edit data
  private $error;
  
  public function action() {
	//delete a row base on language ID
    if ($_GET['a'] == 'erase') {
      DB::sql('delete from ' . TBL_LANGUAGES . ' where id_language = ' . $_GET['ida']);
    }
  }

  public function applyForm($formID = NULL) {
	//add a new language
    if (isset($_POST['langSubmit'])) {
      ////check input data to make sure that there is not special character in query
      $langCode = addslashes($_POST['langCode']);
      $langName = addslashes($_POST['langName']);
	  //show an error message if $langCode or $langName is empty
      if (!$langCode || !$langName) {
        $this->error = '<p class="error">All fields has to be filled.</p>';
      } else {
		//turn off apply Form and update new language information
        $this->showForm = false;
        DB::sql('insert into ' . TBL_LANGUAGES . " values (0, '$langCode', '$langName')", 'id_language');
      }
    }
	
	//edit data base on language ID
    if (isset($_POST['langEdit'])) {
      //$showTable = false;
	  $langCode = addslashes($_POST['langCode']);
      $langName = addslashes($_POST['langName']);
	  //show an error message if $langCode or $langName is empty
      if (!$langCode || !$langName) {
        $this->error = '<p class="error">All fields has to be filled.</p>';
      } else {
      DB::sql('update ' . TBL_LANGUAGES . ' set code = \'' . $_POST['langCode'] . '\', name = \'' . $_POST['langName'] . '\' where id_language = ' . $_POST['ida']);
      $this->showForm = false;
	  }
    }
  }

  //getContent function is called automatically 
  public function getContent() {
    $content = '<h2>Languages</h2>';
    $content .= $this->action();
    $this->applyForm();

    $form = $this->getForm();
    if ($this->showTable) {
      $content .= '<a href="index.php?where=admin&subwhere=langs&a=new">Create language</a>';
    }
    $content .= $form;
    $content .= $this->getTable();

    return $content;
  }

  //create form for add a new language or edit a language
  public function getForm($formID = NULL, $error = NULL) {
    if ($_GET['a'] == 'new' && $this->showForm) {
      $this->showTable = false;

      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Code', 'langCode')->addInput('text', 'langCode', $_POST['langCode']);
      $f->addLabel('Name', 'langName')->addInput('text', 'langName', $_POST['langName']);
      $f->addLabel('&nbsp', 'langSubmit')->addInput('submit', 'langSubmit', 'Create language', array("class" => "submit"));
      $f->endForm();
      
      return $this->error . $f->getForm();
    }


    if ($_GET['a'] == 'edit' && $this->showForm) {
      $this->showTable = false;
      
      $result = DB::sql('select * from ' . TBL_LANGUAGES . ' where id_language = ' . $_GET['ida']);
      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Code', 'langCode')->addInput('text', 'langCode', $result[0]['code']);
      $f->addLabel('Name', 'langName')->addInput('text', 'langName', $result[0]['name']);
      $f->addLabel('&nbsp', 'langEdit')->addInput('submit', 'langEdit', 'Edit language', array("class" => "submit"));
      $f->addInput('hidden', 'ida', $_GET['ida']);
      $f->endForm();
      
      return $this->error . $f->getForm();
    }
  }

  public function getSubmitedForm() {
    
  }

  //create table to show data 
  public function getTable($tableID = NULL) {
    if ($this->showTable) {
      //table of all languages
      $result = DB::sql('select * from ' . TBL_LANGUAGES . ' order by upper(name)');

      if (empty($result)) {
        return '<p>No languages</p>';
      }
      
      $table = '<table>';
      $table .= '<tr>';
      $table .= '<th>Code</th><th>Name</th><th>Edit</th><th>Erase</th>';
      $table .= '</tr>';
      foreach ($result as &$row) {
        $langID = $row['id_language'];
        $table .= '<tr>';
        $table .= '<td>' . $row['code'] . '</td>';
        $table .= '<td>' . $row['name'] . '</td>';
        $table .= '<td><a href="index.php?where=admin&subwhere=langs&a=edit&ida=' . $langID . '">Edit</a></td>';
        $table .= '<td><a href="index.php?where=admin&subwhere=langs&a=erase&ida=' . $langID . '">Erase</a></td>';
        $table .= '</tr>';
      }
      $table .= '';
      $table .= '</table>';

      return $table;
    }
  }

}

?>
